<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script>
    // 调用方法， temp为原始数据, result为树形结构数据

    var data = [
        {'id': 101, 'name': '语文', 'pid': 0},
        {'id': 102, 'name': '语文知识点1', 'pid': 101},
        {'id': 103, 'name': '语文知识点11', 'pid': 102},
        {'id': 104, 'name': '语文知识点2', 'pid': 101},
        {'id': 202, 'name': '数学知识点1', 'pid': 201},
        {'id': 201, 'name': '数学', 'pid': 0},
        {'id': 203, 'name': '数学知识点2', 'pid': 201},
        {'id': 204, 'name': '数学知识点3', 'pid': 201},
        {'id': 302, 'name': '英语知识点1', 'pid': 204}
    ]
    const formatTreeData = (
        data,
        parentID
    ) => {
        const arr = data && data.filter((item) => item.pid === parentID);
        if (arr.length === 0) {
            return [];
        }
        return arr.map((element) => {
            return {
                ...element,
                children: formatTreeData(data, element.id),
            };
        });

    };
    console.log(formatTreeData(data, 0))


    var arrs = [{
        key: '01',
        value: '西施'
    }, {
        key: '02',
        value: '王昭君'
    }, {
        key: '03',
        value: '杨玉环'
    }, {
        key: '04',
        value: '貂蝉'
    }, {
        key: '01',
        value: '西施'
    }, {
        key: '01',
        value: '西施'
    }];
    function fn(arr) {
        var result = [];
        var obj = {};
        arr.map((v, i) => {
            if (!obj[v.key]) {
                result.push(v);
                obj[v.key] = true;
            }
        })
        return result;
    }

    console.log(fn(arrs))

    let nums = [1,3,2,3,2,7,9,7,0,7,8,4,5,4]
    const obj = {}
    nums.forEach((v, i) => {
        if (obj[v]) {
            obj[v]++
        }else {
            obj[v] = 1
        }
    })
    console.log(2222222,obj)

    let nertNum = [1,3,4,7,9]
    let res = nertNum[0]
    nertNum.forEach((v, i, a) => {
        if (Math.abs(v - 11) < Math.abs(res-11)) {
            res = v
        }
    })
    console.log(res)

</script>
</body>
</html>